<template>
    <!-- 页面主体 -->
    <view>
        <!--提示框组件-->
        <u-toast ref="uToast" />
        <!--无网络组件-->
        <u-no-network></u-no-network>
        <!--头部组件-->
        <u-navbar :title="homeTitle" safeAreaInsetTop fixed placeholder>
            <view class="coreshop-navbar-left-slot" slot="left">
                <u-icon name="arrow-left" size="19" @click="goNavigateBack"></u-icon>
                <u-line direction="column" :hairline="false" length="16" margin="0 8px"></u-line>
                <u-icon name="home" size="22" @click="goHome"></u-icon>
            </view>
            <view slot="right">
            </view>
        </u-navbar>
        <!--首页模块化组合组件-->
        <coreshop-page :coreshopdata="pageData"></coreshop-page>
        <!--版权组件-->
        <coreshop-copyright v-if="copy"></coreshop-copyright>
        <!--客服组件-->
        <!-- #ifdef APP-PLUS-NVUE || APP-PLUS -->
        <view class="floatingButton" @click="showChat()">
            <u-icon name="server-man" color="#e54d42" size="60"></u-icon>
        </view>
        <!-- #endif -->
        <!-- #ifdef MP-WEIXIN -->
        <button class="floatingButton" hover-class="none" open-type="contact" bindcontact="showChat" :session-from="kefupara">
            <u-icon name="server-man" color="#e54d42" size="60"></u-icon>
        </button>
        <!-- #endif -->
        <!--返回顶部组件-->
        <u-back-top :scroll-top="scrollTop" :duration="500"></u-back-top>
        <!--弹出框-->
        <coreshop-modal-img :show="modalShow" :src="indexPopupWindowImageUrl" @imgTap="imgTap" @closeTap="closeTap" />
        <!-- 登录提示 -->
        <coreshop-login-modal></coreshop-login-modal>
    </view>
</template>
<script>
    import { mapState } from 'vuex';
    export default {
        data() {
            return {
                currentIndex: 0,
                opacity: 0,
                scrollTop: 0,
                pageData: [],
                pageCode: 'mobile_home', //页面布局编码
                kefupara: '', //客服传递资料
                copy: false,
                shareUrl: this.$globalConstVars.shareUrl,
                isScorll: false,
                homeTitle: '',
                modalShow: false,
            };
        },
        updated() {
            this.copy = true;
        },
        computed: {
            ...mapState({
                hasLogin: state => state.hasLogin,
                userInfo: state => state.userInfo,
            }),
            hasLogin: {
                get() {
                    return this.$store.state.hasLogin;
                },
                set(val) {
                    this.$store.commit('hasLogin', val);
                }
            },
            userInfo: {
                get() {
                    return this.$store.state.userInfo;
                },
                set(val) {
                    this.$store.commit('userInfo', val);
                }
            },
            // 获取店铺联系人手机号
            shopMobile() {
                return this.$store.state.config.shopMobile || 0;
            },
            modalShowData() {
                this.modalShow = this.$store.state.config.showIndexPopupWindow == 1;
                return this.$store.state.config.showIndexPopupWindow == 1;
            },
            indexPopupWindowHrefUrl() {
                return this.$store.state.config.indexPopupWindowHrefUrl;
            },
            indexPopupWindowImageUrl() {
                return this.$store.state.config.indexPopupWindowImageUrl;
            }
        },
        onLoad(e) {
            //增加页面编码，可自定义编码
            if (e.pageCode) {
                this.pageCode = e.pageCode;
            }
            this.initData();
        },
        methods: {
            imgTap() {
                this.modalShow = false;
                var val = this.indexPopupWindowHrefUrl;
                if (this.indexPopupWindowHrefUrl.indexOf('http') != -1) {
                    // #ifdef APP-PLUS || APP-PLUS-NVUE || MP
                    this.$u.route('/pages/webview/webview', { src: val });
                    // #endif
                } else {
                    // #ifdef APP-PLUS || APP-PLUS-NVUE || MP
                    if (val == '/pages/index/default/default' || val == '/pages/category/index/index' || val == '/pages/index/cart/cart' || val == '/pages/index/member/member') {
                        this.$u.route({ type: 'switchTab', url: val });
                        return;
                    } else if (val.indexOf('/pages/coupon/coupon?id=') > -1) {
                        var id = val.replace('/pages/coupon/coupon?id=', "");
                        this.receiveCoupon(id)
                    } else {
                        this.$u.route(val);
                        return;
                    }
                    // #endif
                }
            },
            closeTap() {
                this.modalShow = false;
            },
            // 首页初始化获取数据
            initData() {
                uni.showLoading({
                    title: '加载中'
                });
                //获取首页配置
                this.$u.api.getPageConfig({ code: this.pageCode }).then(res => {
                    if (res.status == true) {
                        this.pageData = res.data.items;
                        this.homeTitle = res.data.name;
                    }
                    setTimeout(function () {
                        uni.hideLoading();
                    }, 1000);
                });

                var _this = this;
                if (this.$db.get('userToken')) {
                    this.$u.api.userInfo().then(res => {
                        if (res.status) {
                            _this.userInfo = res.data;
                            _this.hasLogin = true;
                            // #ifdef MP-WEIXIN
                            //微信小程序打开客服时，传递用户信息
                            var kefupara = {};
                            kefupara.nickName = res.data.nickName;
                            kefupara.tel = res.data.mobile;
                            _this.kefupara = JSON.stringify(kefupara);
                            //console.log(_this.kefupara);
                            // #endif
                        }
                    });
                }
                this.getShareUrl();
            },
            //在线客服
            showChat() {
                // #ifdef APP-PLUS || APP-PLUS-NVUE
                this.$u.route('/pages/member/customerService/index');
                // #endif
            },
            //获取分享URL
            getShareUrl() {
                let data = {
                    client: this.$globalConstVars.shareClient.wxMiNiProgram,
                    url: this.$globalConstVars.shareUrl,
                    type: this.$globalConstVars.shareModel.url,
                    page: this.$globalConstVars.shareType.index,
                };
                let userToken = this.$db.get('userToken');
                if (userToken && userToken != '') {
                    data.token = userToken
                }
                this.$u.api.share(data).then(res => {
                    this.shareUrl = res.data
                });
            }
        },
        onPullDownRefresh() {
            this.initData();
            uni.stopPullDownRefresh();
        },
        //分享
        onShareAppMessage(res) {
            return {
                title: this.$store.state.config.shareTitle,
                imageUrl: this.$store.state.config.shareImage,
                path: this.shareUrl
            }
        },
        onShareTimeline(res) {
            return {
                title: this.$store.state.config.shareTitle,
                imageUrl: this.$store.state.config.shareImage,
                path: this.shareUrl
            }
        },
        onPageScroll: function (e) {
            this.isScorll = e.scrollTop > 100 ? true : false;
            this.scrollTop = e.scrollTop;
            if (e.scrollTop <= 100) {
                this.opacity = e.scrollTop / 100;
            } else if (this.scrollTop > 100) {
                this.opacity = 1;
            }
        },
    };
</script>

<style lang="scss">
</style>
